package com.wpre.mybatisdemo.dao;

import com.wpre.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-11-02
 * Time: 16:25
 */
@Mapper
public interface UserInfoMapper {
    @Select("select * from user_info")
    List<UserInfo> selectAll();

    //查询
    //    @Select("select * from user_info where id=4;")
    //    UserInfo selectById();
    //通过参数传递
    @Select("select * from user_info where id=#{id}")
    UserInfo selectById(Integer id);

    //非唯一主键查询, 返回结果建议使用List
    //        @Select("select * from user_info where age= #{age} and gender = #{gender}")  //1. 正确的写法  推荐写法
    //        @Select("select * from user_info where age= #{age1} and gender = #{gender}")  // 参数绑定异常
    //        @Select("select * from user_info where age= #{param2} and gender = #{param1}")  //2. 可以正确执行, 但是不推荐

    @Select("select * from user_info where age= #{age} and gender = #{gender}")
        //3.参数重命名  推荐写法, 参数和注解名称保持一致
        //    List<UserInfo> selectUserByAgeAndGender(@Param("gender") Integer gender1, @Param("age") Integer age1);
    List<UserInfo> selectUserByAgeAndGender(@Param("gender") Integer gender, @Param("age") Integer age);

    //下划线命名法转换为驼峰命名法
    @Results(id = "map", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })//建立映射
    @Select("select * from user_info")
    List<UserInfo> selectAll2();

    @ResultMap("map")//注解复用
    @Select("select * from user_info")
    List<UserInfo> selectAll3();

//插入

    @Insert("insert into user_info(username,`password`,age,gender) values (#{username},#{password},#{age},#{gender})")
    Integer insertUserInfo(UserInfo userInfo);

    //对象重命名
    @Insert("insert into user_info(username,`password`,age,gender) values (#{Info.username},#{Info.password},#{Info.age},#{Info.gender})")
    Integer insertUserInfo2(@Param("Info") UserInfo userInfo);//返回值是影响的行数


    //获取自增的id
    @Options(useGeneratedKeys = true, keyProperty = "id")//
    @Insert("insert into user_info(username,`password`,age,gender) values (#{Info.username},#{Info.password},#{Info.age},#{Info.gender})")
    Integer insertUserInfo3(@Param("Info") UserInfo userInfo);//返回值是影响的行数

//删除

    @Delete("delete from user_info where id = #{id}")
    Integer delete(Integer id);//返回值:影响的行数

//修改

    @Update("update user_info set password = #{password} where id =#{id}")
    Integer update(Integer id, String password);

    //参数为对象
    @Update("update user_info set password = #{password} where id =#{id}")
    Integer update2(UserInfo userInfo);

    //排序
    @Select("select * from user_info order by id ${order};")
    List<UserInfo> selectUsernameByOrder(String order);

    //like查询
    @Select("select * from user_info where username like CONCAT('%','${name}','%') ")
    List<UserInfo> selectByLike(String name);

}
